<?php
include_once( "common.php" );

# Make the operation fail
function fail( $message )
{
    print $message;
    exit();
}

# Validate the revision
if ( ! is_numeric( $_GET["rev"] ) )
    fail( "invalid revision number" );

# Validate the branch
if ( $_GET["branch"] != addslashes( $_GET["branch"] ) )
    fail( "invalid branch" );

# Verify that the user's authentication token is valid
if ( $_GET["auth"] != addslashes( $_GET["auth"] ) or db_num_rows( db_query( "SELECT User.id FROM User WHERE User.feed = '{$_GET["auth"]}'" ) ) != 1 )
    fail( "invalid authenication token" );

# Validate the bugs
foreach ( $_GET["id"] as $bug )
{
    # Make sure the user can update this bug and that the bug is not already closed
    if ( ! isValidEntry( "Bug", $bug ) or db_num_rows( db_query( "SELECT User.id FROM User, UserProject, Bug WHERE Bug.status != 3 AND Bug.id = {$bug} AND Bug.project = UserProject.project AND User.id = UserProject.user AND User.feed = '{$_GET["auth"]}'" ) ) != 1 )
        fail( "invalid bug entered" );
}

# All is well, so close the bugs
foreach ( $_GET["id"] as $bug )
{
    # Mark the bug as closed
    db_query( "UPDATE Bug SET Bug.status = 3 WHERE Bug.id = {$bug}" );

    # Get the user's information
    $result = db_query( "SELECT User.id FROM User WHERE User.feed = '{$_GET["auth"]}'" );
    $row = db_fetch_object( $result );

    # Log it
    $changes = array();
    $changes["status"] = 3;
    addLog( $bug, "Closed from Bazaar (branch {$_GET["branch"]}, revision {$_GET["rev"]})", $changes, $row->id );
}
?>
